<?php

/**
 * 前台用户认证
 * @author zjx
 *
 */
class UserIdentity extends CUserIdentity {
	
	public $response = '';
	protected $_identity = '';
	public $login_method = 0;
	public $group_id;
	public $isEnableCommunity=false;//是否采用社区化登录, 如:qq、人人网、新浪等
	public function authenticate() {
		try {
			$this->login_method=trim($this->login_method);
			$this->username=trim($this->username);
			$this->password=trim($this->password);
			$data=array();
				$user = new User ();
				$username=$data['account'];
				$account = $user->find ( "username=:username and userpwd=:userpwd", array (":username" => $username,":userpwd"=>$this->password));
				if (is_null ( $account )) {
					return UtilD::handleResult ( 0, '用户名或者密码不正确！' );
				}
				if ($account->sta == '0') {
					return UtilD::handleResult ( 0, '此账号已被冻结！' );
				}
				if(is_null($account->id)){
					return UtilD::handleResult ( 0, '帐号登陆异常！' );
				}
				$this->group_id=$account->group_id;//防止注册未成功登录的用户，用户数据是空的。
				if(empty($data ['currentIP'])){
					 $data ['currentIP']=Yii::App ()->request->getUserHostAddress ();
				}
				$mflag=(isset($data ['lastip']) && $data ['lastip']=== Yii::App ()->request->getUserHostAddress ());
				$this->_identity = $account->id;
				$states = array ('tickets' => $passport->tickets, 'login_method' => $this->login_method, 'group_id'=>$this->group_id );
				$this->setPersistentStates ( $states );
				$login_nums=$account->login_nums + 1;
				$update_time = time ();
				$last_loginip = Yii::App ()->request->getUserHostAddress ();
				$account->setIsNewRecord ( false );
				$attributes=array('login_nums'=>$login_nums, 'update_time'=>time(), 'last_loginip'=>$last_loginip,'last_logintime'=>time());
				$rs=$account->updateByPk($this->_identity, $attributes);
				$datas = $account->getAttributes();
				$result_data = XxteaD::decrypt ( $datas, Yii::app ()->params ['frontpassportkey']);
				$data = CJSON::decode ( $result_data );
				$data [WebUserD::STORAGE_KEY] = CJSON::decode ( $data [WebUserD::STORAGE_KEY] );
				$logAttributes['userid']=$this->getId();
				$logAttributes['username']=$this->username;
				
				if ($rs) {
					Yii::app()->backlog->logging($logAttributes,$mflag);
					return UtilD::handleResult ( 1, '登录成功！' );
				}else{
					$logAttributes['sta']=false;
					Yii::app()->backlog->logging($logAttributes,false);
					return UtilD::handleResult ( 0, '登录失败！' );
				}
			
		} catch ( Exception $e ) {
			return UtilD::handleResult ( 0, '系统忙，请稍后再试！' . $e->getMessage()  );
		}
	}
	
	public function getId() {
		return $this->_identity;
	}

}